package tinsiec;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Main
{
    public static void main(String[] args)
    {
        final BlockingQueue<MessageInfo> recvQueue = new LinkedBlockingQueue<MessageInfo>();
        final BlockingQueue<MessageInfo> sendQueue = new LinkedBlockingQueue<MessageInfo>();
        
        MulticastModule mm;
        try
        {
            Enumeration<NetworkInterface> eni = NetworkConfig.getAvailableInterfaces();
            while(eni.hasMoreElements())
            {
                System.out.println("Interface: " + eni.nextElement().getDisplayName());
            }
            NetworkConfig.setSelectedInterface(NetworkInterface.getByName("eth0"));
            mm = new MulticastModule("224.0.0.117", 15500);

            mm.setRecvQueue(recvQueue);
            mm.setSendQueue(sendQueue);
            Thread t = new Thread(new Runnable()
            {
                public void run()
                {
                    System.out.println("waiting for messages...");
                    while(true)
                    {
                        try
                        {
                            MessageInfo message = recvQueue.take();
                            System.out.println("[" + message.getSenderIP() + "]:" + message.getContent());
                        }
                        catch(InterruptedException ex)
                        {
                            ex.printStackTrace();
                            return;
                        }
                    }
                }
            });
            t.start();
            System.out.println("Adres: " + NetworkConfig.getLocalIP());
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
            return;
        }
        mm.startListening();

        while(true)
        {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            try
            {
                byte[] data = new byte[65535];
                int length = System.in.read(data);
                if(length > 0)
                {
                    String command = new String(data, 0, length - 1);
                    if(command.equals("stop"))
                        mm.stopListening();
                    else if(command.equals("start"))
                        mm.startListening();
                    else if(command.startsWith("if"))
                        NetworkConfig.setSelectedInterface(NetworkInterface.getByName(command.substring(3)));
                    else
                        sendQueue.add(new MessageInfo(command));
                }
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }

        }

    }

}
